﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;
using UnityEngine;
using Yoozoo.Gameplay.RTS.Proto;
using Yoozoo.Mars.Got;

namespace Yoozoo.Gameplay.RTS
{
    public class SmugglerHud : MonsterHud
    {
        private SmugglerHudLod0 SmugglerHudLod0;
        private GameObject SmugglerHudLod0Obj;
        private GameObject SumgglerHudLod1Obj;
        private MonsterHudLod1 MonsterHudLod1;
        private bool isInBattle = false;
        public override void Initialize(int team)
        {
            BaseInitialize(team);

            var hudManager = ArmyManager.Instance.HudManager;

            if (SmugglerHudLod0Obj == null)
            {
                SmugglerHudLod0Obj = hudManager.GetHud(HudManager.SmuggleHudLod0);
            }
            if (SumgglerHudLod1Obj == null)
            {
                SumgglerHudLod1Obj = hudManager.GetHud(HudManager.SmuggleHudLod1);
            }

            SmugglerHudLod0 = SmugglerHudLod0Obj?.GetComponent<SmugglerHudLod0>();

            MonsterHudLod1 = SumgglerHudLod1Obj?.GetComponent<MonsterHudLod1>();
            InitAlpha();
            SmugglerHudLod0.timeTxt.enabled = false;
        }

        // public override void UpdateMonsterInfo()
        // {
        //     
        // }

        public override void Dispose()
        {
            SmugglerHudLod0.SetHpActive(false);
            SmugglerHudLod0.SetLogoActive(true);
            // 释放
            var hudManager = ArmyManager.Instance.HudManager;
            if (SmugglerHudLod0Obj)
            {
                hudManager.RecoverHud(HudManager.SmuggleHudLod0, SmugglerHudLod0Obj);
                SmugglerHudLod0Obj = null;
            }

            if (SumgglerHudLod1Obj)
            {
                hudManager.RecoverHud(HudManager.SmuggleHudLod1, SumgglerHudLod1Obj);
                SumgglerHudLod1Obj = null;
            }
            Lod1 = null;
            MarchHudLod0 = null;
            MarchEntity = null;
            hasSetParent = false;
            MonsterHudLod1 = null;
            RadarHudDispose();
            BaseDispose();
            Timers.inst.Remove(CalSmuggleTime);
        }
        public override void RoundFight(int soldierCurrentCount, int totalSoldierCount, bool isInBattle)
        {
            this.isInBattle = isInBattle;
            if (isInBattle)
            {
                SmugglerHudLod0.SetHpActive(true);
                SmugglerHudLod0.SetHp(soldierCurrentCount, totalSoldierCount);
            }
            else
            {
                SmugglerHudLod0.SetHpActive(false);
            }
            SmugglerHudLod0.SetLogoActive(!isInBattle);
        }
        private Vector3 smugglerHudLod0Offset = new Vector3(0f, 0.05f, 0f);
        private Vector3 smugglerHudLod1Offset = new Vector3(0f, 0.05f, 0f);
        private Vector3 smugglerHudlod1Scale = new Vector3(0.1f, 0.1f, 0.1f);
        
        protected override void InitParentScaleAndPosition()
        {
            if (!hasSetParent)
            {
                SumgglerHudLod1Obj.transform.localScale = smugglerHudlod1Scale;
            }
           
            hasSetParent = true;
        }
        public override void UpdateFollowPos()
        {
            if (hasSetParent)
            {
                var parentPos = MarchEntity.Transform.position;
                /*if (SmugglerHudLod0Obj != null)
                {
                    SmugglerHudLod0Obj.transform.localPosition = smugglerHudLod0Offset + parentPos;
                }*/
                if (SmugglerHudLod0 != null)
                {
                    SmugglerHudLod0.SetLocalPosition(smugglerHudLod0Offset + parentPos);
                }

                if (SumgglerHudLod1Obj != null)
                {
                    SumgglerHudLod1Obj.transform.localPosition = smugglerHudLod1Offset+ parentPos;
                }
                base.UpdateFollowPos();
            }
        }
        protected override void OnPlayerNameVisibleChanged(bool value)
        {
            if (SmugglerHudLod0Obj != null)
            {
                // SmugglerHudLod0Obj.SetActive(value);
            }
        }
        protected override void OnCommanderHeadInBattleStateChanged(bool value)
        {
            if (SmugglerHudLod0Obj != null)
            {
                // SmugglerHudLod0.SetLogoActive(!value);
            }
        }
        
        public override void UpdateMonsterInfo()
        {
            if (MarchEntity.RtsServerInfo.marchStatus == MarchStatus.INIT_0)
            {
                var leftTime = MarchEntity.RtsServerInfo.expireMills - TimeUtils.GetServerTimeMs();
                if (leftTime > 0)
                {
                    SmugglerHudLod0.timeTxt.text = ""; // 显示之前先清除一下内容
                    SmugglerHudLod0.timeTxt.enabled = true;
                   
                    SmugglerHudLod0.timeBg.enabled = true;
                    Timers.inst.Remove(CalSmuggleTime);
                    Timers.inst.Add(1,-1,CalSmuggleTime);
                }
                else
                {
                    SmugglerHudLod0.timeTxt.enabled = false;
                    SmugglerHudLod0.timeBg.enabled = false;
                }
                SmugglerHudLod0.SetHpActive(false);
                isInBattle = false;
            }
            else
            {
                SmugglerHudLod0.timeTxt.enabled = false;
                SmugglerHudLod0.timeBg.enabled = false;
            }
        }

        private void CalSmuggleTime(object param)
        {
            var leftTime = MarchEntity.RtsServerInfo.expireMills - TimeUtils.GetServerTimeMs();
            if (leftTime > 0)
            {
                SmugglerHudLod0.timeTxt.text = TimeUtils.MillisecondsToTime(leftTime);
            }
            else
            {
                Timers.inst.Remove(CalSmuggleTime);
            }
        }

        public override void UpdatePlayerName()
        {
            var level = MarchEntity.RtsServerInfo.level;
            SmugglerHudLod0.SetLevel(level);
            if (MonsterHudLod1!=null)
            {
                MonsterHudLod1.LevelText.text = level.ToString();
            }

            SmugglerHudLod0.SetHp(MarchEntity.RtsServerInfo.soldierCount, MarchEntity.RtsServerInfo.initSoldierCount);
            if (MarchEntity.RtsServerInfo.soldierCount < MarchEntity.RtsServerInfo.initSoldierCount)
            {
                SmugglerHudLod0.SetHpActive(true);
            }
            else
            {
                SmugglerHudLod0.SetHpActive(false);
            }
            SmugglerHudLod0.SetLogoActive(!isInBattle);
        }
        protected override void SetLod0Visible(MarchContext marchContext, bool force = false)
        {
            var isShowLod0 = marchContext.LodLevel < WorldEntry.MaxShowMarchLodLevel;
            bool lod0Visible = isShowLod0;
            base.SetHudLodScale(GetLodLowMonsterScaleType(), SmugglerHudLod0Obj, marchContext);
            UpdateFollowPos();
            if (_lod0Visible != lod0Visible || force)
            {
                _lod0Visible = lod0Visible;
                SmugglerHudLod0.SetActive(lod0Visible);
                if (_lod0Visible)
                {
                    SmugglerHudLod0.SetLogoActive(!MarchEntity.MarchInfo.IsInBattleState);
                }
            }
            bool lod0HpVisible = lod0Visible && isInBattle;
            if (lod0HpVisible != SmugglerHudLod0.HpBarVisible)
            {
                SmugglerHudLod0.SetHpActive(lod0HpVisible); 
                if (lod0HpVisible)
                {
                    SmugglerHudLod0.SetHp(MarchEntity.RtsServerInfo.soldierCount, MarchEntity.RtsServerInfo.initSoldierCount);
                }
            }
        }

        public override void OnCameraPosUpdated(MarchContext marchContext)
        {
            int currentLodLevel = marchContext.LodLevel;
            float currentLodPercent = marchContext.LodPercent;
            if (MonsterHudLod1)
            {
                if (currentLodLevel >= WorldEntry.MaxShowMarchLodLevel)
                {
                    MonsterHudLod1.SetLevelVisible(true);
                    MonsterHudLod1.SetIconVisible(true);
                    SetRadarIconActive(false);
                }
                else
                {
                    MonsterHudLod1.SetLevelVisible(false);
                    MonsterHudLod1.SetIconVisible(false);
                    SetRadarIconActive(!MarchEntity.MarchInfo.IsInBattleState);
                }
            }
        }

        protected override void OnLod1ScaleChanged(MarchContext marchContext)
        {
            if (SumgglerHudLod1Obj!=null)
            {
                SetHudLodScale(GetLodHighMonsterScaleType(), SumgglerHudLod1Obj, marchContext);
            }
        }
        protected override void OnLod0ScaleChanged(MarchContext marchContext)
        {
            if (SmugglerHudLod0Obj != null && isInBattle)
            {
                SetHudLodScale(GetLodLowMonsterScaleType(), SmugglerHudLod0Obj, marchContext);
            }
            updateRadarHud(marchContext);
        }

        public void SetLod0Icon(string icon)
        {
            if (SmugglerHudLod0)
            {
                SmugglerHudLod0.SetItemIcon(icon);
            }
        }

        public void UpdateSmugglerWorldPosTip(Int64 troopId, bool bShow, string tip)
        {
            if (SmugglerHudLod0)
            {
                SmugglerHudLod0.UpdateSmugglerWorldPosTip(troopId,  bShow, tip);
            }
        }
    }
}
